package com.zscat.mallplus.bill.service.impl;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.bill.entity.*;
import com.zscat.mallplus.bill.mapper.*;
import com.zscat.mallplus.bill.service.IBillAftersalesService;
import com.zscat.mallplus.oms.entity.OmsDeliveryExpressCompany;
import com.zscat.mallplus.oms.mapper.OmsDeliveryExpressCompanyMapper;
import com.zscat.mallplus.sys.entity.SysUser;
import com.zscat.mallplus.util.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 退货单表 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-09-16
 */
@Service
public class BillAftersalesServiceImpl extends ServiceImpl<BillAftersalesMapper, BillAftersales> implements IBillAftersalesService {
    @Resource
    private BillAftersalesMapper billAftersalesMapper;

    @Resource
    private BillReshipMapper billReshipMapper;


    @Resource
    private OmsDeliveryExpressCompanyMapper omsDeliveryExpressCompanyMapper;

    @Resource
    private BillExchangeMapper billExchangeMapper;

    @Resource
    private BillAftersalesItemsMapper billAftersalesItemsMapper;

    @Resource
    private BillAftersalesOperateHistoryMapper billAftersalesOperateHistoryMapper;


    @Override
    public Object getReturnedGoodsCount(Integer storeId) {
        int count = billAftersalesMapper.selectCount(new QueryWrapper<BillAftersales>().eq("store_id", storeId));
        return count;
    }

    @Override
    @Transactional
    public void doRejectReturn(Long aftersalesId, String remark) {
        SysUser user = UserUtils.getCurrentMember();
        BillAftersales billAftersales = billAftersalesMapper.selectById(aftersalesId);

        BillAftersalesOperateHistory billAftersalesOperateHistory = new BillAftersalesOperateHistory();
        billAftersalesOperateHistory.setAftersalesId(aftersalesId);

        billAftersalesOperateHistory.setPreStatus(billAftersales.getAftersalesStatus());
        //billAftersalesOperateHistory.setStoreId(billAftersales.getStoreId());
        billAftersalesOperateHistory.setCreateTime(new Date());
        billAftersalesOperateHistory.setNote(remark);
        billAftersalesOperateHistory.setOperateMan(user.getUsername());

        // 修改售后单状态
        billAftersales.setStatus(3);
        billAftersales.setRefundStatus(1);
        billAftersales.setAftersalesStatus(1);
        billAftersalesMapper.updateById(billAftersales);
        billAftersalesOperateHistory.setAftersalesStatus(1);
        // 修改售后商品状态
        List<BillAftersalesItems> billAftersalesItemList = billAftersalesItemsMapper.selectList(new QueryWrapper<BillAftersalesItems>().eq("aftersales_id", billAftersales.getAftersalesId()));
        for (BillAftersalesItems billAftersalesItems : billAftersalesItemList) {
            // 0正常 1退换货中2换货成功3退款成功4申请拒绝
            billAftersalesItems.setStatus(4);
            billAftersalesItemsMapper.updateById(billAftersalesItems);
        }
        // 添加售后操作记录
        billAftersalesOperateHistoryMapper.insert(billAftersalesOperateHistory);
    }

    @Override
    @Transactional
    public void handleDeliverGoods(Long aftersalesId, String sellerLogiCode, String sellerLogiNo) {
        SysUser user = UserUtils.getCurrentMember();
        BillAftersales billAftersales = billAftersalesMapper.selectById(aftersalesId);

        BillAftersalesOperateHistory billAftersalesOperateHistory = new BillAftersalesOperateHistory();
        billAftersalesOperateHistory.setAftersalesId(aftersalesId);

        billAftersalesOperateHistory.setPreStatus(billAftersales.getAftersalesStatus());
        //billAftersalesOperateHistory.setStoreId(billAftersales.getStoreId());
        billAftersalesOperateHistory.setCreateTime(new Date());
        billAftersalesOperateHistory.setOperateMan(user.getUsername());

        // 修改售后单状态
        billAftersales.setStatus(1);
        billAftersales.setRefundStatus(1);
        billAftersales.setAftersalesStatus(5);// 卖家已发货
        billAftersalesMapper.updateById(billAftersales);
        billAftersalesOperateHistory.setAftersalesStatus(5);  // 卖家已发货
        // 修改售后商品状态
        List<BillAftersalesItems> billAftersalesItemList = billAftersalesItemsMapper.selectList(new QueryWrapper<BillAftersalesItems>().eq("aftersales_id", billAftersales.getAftersalesId()));
        for (BillAftersalesItems billAftersalesItems : billAftersalesItemList) {
            // 0正常 1退换货中2换货成功3退款成功4申请拒绝
            billAftersalesItems.setStatus(1);
            billAftersalesItemsMapper.updateById(billAftersalesItems);
        }
        // 添加售后操作记录
        billAftersalesOperateHistoryMapper.insert(billAftersalesOperateHistory);

        // 修改换货表 卖家发货的信息：物流信息、
        BillExchange billExchange = billExchangeMapper.selectOne(new QueryWrapper<BillExchange>().eq("aftersales_id", billAftersales.getAftersalesId()));
        OmsDeliveryExpressCompany omsDeliveryExpressCompany = new OmsDeliveryExpressCompany();
        omsDeliveryExpressCompany.setComCode(sellerLogiCode);
        OmsDeliveryExpressCompany info = omsDeliveryExpressCompanyMapper.selectOne(new QueryWrapper<>(omsDeliveryExpressCompany));
        billExchange.setSellerLogiCode(sellerLogiCode);
        billExchange.setSellerLogiNo(sellerLogiNo);
        billExchange.setSellerLogiName(info.getExpress());
        // 5卖家已发货中
        billExchange.setStatus(5);
        billExchangeMapper.updateById(billExchange);
    }

    /**
     * 卖家确认收货
     *
     * @param aftersalesId
     * @param remark
     */
    @Override
    @Transactional
    public void confirmReceipt(Long aftersalesId, String remark) {
        SysUser user = UserUtils.getCurrentMember();
        BillAftersales billAftersales = billAftersalesMapper.selectById(aftersalesId);
        BillAftersalesOperateHistory billAftersalesOperateHistory = new BillAftersalesOperateHistory();
        billAftersalesOperateHistory.setAftersalesId(aftersalesId);
        billAftersalesOperateHistory.setPreStatus(billAftersales.getAftersalesStatus());
        //billAftersalesOperateHistory.setStoreId(billAftersales.getStoreId());
        billAftersalesOperateHistory.setCreateTime(new Date());
        billAftersalesOperateHistory.setOperateMan(user.getUsername());
        if (billAftersales.getType().equals(2)) {// 退货
            // 修改售后单状态
            billAftersales.setStatus(2);       // 2已完成
            billAftersales.setRefundStatus(2); // 已退款
            billAftersales.setAftersalesStatus(4);// 卖家已收货
            billAftersalesMapper.updateById(billAftersales);
            billAftersalesOperateHistory.setAftersalesStatus(4);  // 卖家已收货
            // 修改售后商品状态
            List<BillAftersalesItems> billAftersalesItemList = billAftersalesItemsMapper.selectList(new QueryWrapper<BillAftersalesItems>().eq("aftersales_id", billAftersales.getAftersalesId()));
            for (BillAftersalesItems billAftersalesItems : billAftersalesItemList) {
                // 0正常 1退换货中2换货成功3退款成功4申请拒绝
                billAftersalesItems.setStatus(3);  // 3退款成功
                billAftersalesItemsMapper.updateById(billAftersalesItems);
            }
            // 添加售后操作记录
            billAftersalesOperateHistoryMapper.insert(billAftersalesOperateHistory);

            // 修改退货表状态
            BillReship billReship = billReshipMapper.selectOne(new QueryWrapper<BillReship>().eq("aftersales_id", billAftersales.getAftersalesId()));
            billReship.setStatus(3);// 3=已收退货
            billReshipMapper.updateById(billReship);
        } else { // 换货
            // 修改售后单状态
            billAftersales.setStatus(1);
            billAftersales.setRefundStatus(1); // 未退款
            billAftersales.setAftersalesStatus(4);// 卖家已收货
            billAftersalesMapper.updateById(billAftersales);
            billAftersalesOperateHistory.setAftersalesStatus(4);  // 卖家已收货
            // 修改售后商品状态
            List<BillAftersalesItems> billAftersalesItemList = billAftersalesItemsMapper.selectList(new QueryWrapper<BillAftersalesItems>().eq("aftersales_id", billAftersales.getAftersalesId()));
            for (BillAftersalesItems billAftersalesItems : billAftersalesItemList) {
                // 0正常 1退换货中2换货成功3退款成功4申请拒绝
                billAftersalesItems.setStatus(1);
                billAftersalesItemsMapper.updateById(billAftersalesItems);
            }
            // 添加售后操作记录
            billAftersalesOperateHistoryMapper.insert(billAftersalesOperateHistory);

            // 修改换货表 卖家收货状态
            BillExchange billExchange = billExchangeMapper.selectOne(new QueryWrapper<BillExchange>().eq("aftersales_id", billAftersales.getAftersalesId()));
            //  3卖家已收货
            billExchange.setStatus(6);
            billExchangeMapper.updateById(billExchange);
        }
    }

    /**
     * 确认退换货
     *
     * @param aftersalesId
     * @param remark
     */
    @Override
    @Transactional
    public void confirmReturn(Long aftersalesId, String remark) {
        SysUser user = UserUtils.getCurrentMember();
        BillAftersales billAftersales = billAftersalesMapper.selectById(aftersalesId);
        BillAftersalesOperateHistory billAftersalesOperateHistory = new BillAftersalesOperateHistory();
        billAftersalesOperateHistory.setAftersalesId(aftersalesId);
        billAftersalesOperateHistory.setPreStatus(billAftersales.getAftersalesStatus());
        //billAftersalesOperateHistory.setStoreId(billAftersales.getStoreId());
        billAftersalesOperateHistory.setCreateTime(new Date());
        billAftersalesOperateHistory.setOperateMan(user.getUsername());
        if (billAftersales.getType().equals(2)) {// 退货
            // 修改售后单状态
            billAftersales.setStatus(1);  // 1退换货中
            billAftersales.setRefundStatus(1); // 未退款
            billAftersales.setAftersalesStatus(2);// 2待买家发货
            billAftersalesMapper.updateById(billAftersales);
            billAftersalesOperateHistory.setAftersalesStatus(2);  // 2待买家发货
            // 修改售后商品状态
            List<BillAftersalesItems> billAftersalesItemList = billAftersalesItemsMapper.selectList(new QueryWrapper<BillAftersalesItems>().eq("aftersales_id", billAftersales.getAftersalesId()));
            for (BillAftersalesItems billAftersalesItems : billAftersalesItemList) {
                // 0正常 1退换货中2换货成功3退款成功4申请拒绝
                billAftersalesItems.setStatus(1);
                billAftersalesItemsMapper.updateById(billAftersalesItems);
            }
            // 添加售后操作记录
            billAftersalesOperateHistoryMapper.insert(billAftersalesOperateHistory);

            // 添加退货表记录
            BillReship billReship = new BillReship();

            billReship.setOrderId(billAftersales.getOrderId());
            billReship.setAftersalesId(String.valueOf(billAftersales.getAftersalesId()));
            billReship.setUserId(Integer.parseInt(billAftersales.getUserId().toString()));
            billReship.setStatus(1);
            billReship.setMemo(remark);
            billReship.setCtime(new Date());
            billReship.setUtime(new Date());
            billReship.setHandleTime(new Date());
            billReship.setHandleMan(user.getUsername());
            billReship.setReturnName(billAftersales.getLinkMan());
            billReship.setReturnPhone(billAftersales.getLinkPhone());
            billReshipMapper.insert(billReship);
        } else { // 换货
            // 修改售后单状态
            billAftersales.setStatus(1);
            billAftersales.setRefundStatus(1); // 未退款
            billAftersales.setAftersalesStatus(2);// 2待买家发货
            billAftersalesMapper.updateById(billAftersales);
            billAftersalesOperateHistory.setAftersalesStatus(4);  // 卖家已收货
            // 修改售后商品状态
            List<BillAftersalesItems> billAftersalesItemList = billAftersalesItemsMapper.selectList(new QueryWrapper<BillAftersalesItems>().eq("aftersales_id", billAftersales.getAftersalesId()));
            for (BillAftersalesItems billAftersalesItems : billAftersalesItemList) {
                // 0正常 1退换货中2换货成功3退款成功4申请拒绝
                billAftersalesItems.setStatus(1);
                billAftersalesItemsMapper.updateById(billAftersalesItems);
            }
            // 添加售后操作记录
            billAftersalesOperateHistoryMapper.insert(billAftersalesOperateHistory);

            // 添加换货表记录
            BillExchange billExchange = new BillExchange();
            billExchange.setOrderId(billAftersales.getOrderId());
            billExchange.setAftersalesId(String.valueOf(billAftersales.getAftersalesId()));
            billExchange.setUserId(Integer.parseInt(billAftersales.getUserId().toString()));
            billExchange.setStatus(1);//状态：1 待换货  2买家已发货  3卖家已收货  4 卖家处理换货中   5卖家已发货中  6买家已收货 7换货成功
            billExchange.setBuyShipAddress(billAftersales.getAddress());
            billExchange.setBuyShipMobile(billAftersales.getLinkPhone());
            billExchange.setBuyShipName(billAftersales.getLinkMan());
            billExchange.setCtime(new Date());
            billExchange.setUtime(new Date());
            billExchangeMapper.insert(billExchange);
        }
    }

}
